<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="标签名" prop="name">
      <el-input v-model="dataForm.name" placeholder="标签名"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="description">
      <el-input v-model="dataForm.description" placeholder="描述"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          description: '',
          createBy: '',
          createTime: '',
          lastUpdateBy: '',
          lastUpdateTime: '',
          delFlag: ''
        },
        dataRule: {
          name: [
            { required: true, message: '标签名不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          lastUpdateBy: [
            { required: true, message: '更新人不能为空', trigger: 'blur' }
          ],
          lastUpdateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '是否删除  -1：已删除  0：正常不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$api.Label.info(this.dataForm.id).then((data) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.label.name
                this.dataForm.description = data.label.description
                this.dataForm.createBy = data.label.createBy
                this.dataForm.createTime = data.label.createTime
                this.dataForm.lastUpdateBy = data.label.lastUpdateBy
                this.dataForm.lastUpdateTime = data.label.lastUpdateTime
                this.dataForm.delFlag = data.label.delFlag
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {

              let data = {
                  'id': this.dataForm.id || undefined,
                  'name': this.dataForm.name,
                  'description': this.dataForm.description,
                  'createBy': this.dataForm.createBy,
                  'createTime': this.dataForm.createTime,
                  'lastUpdateBy': this.dataForm.lastUpdateBy,
                  'lastUpdateTime': this.dataForm.lastUpdateTime,
                  'delFlag': this.dataForm.delFlag
              };
              let resultAction;
              if (!this.dataForm.id) {
                  resultAction = this.$api.Label.save(data);
              } else {
                  resultAction = this.$api.Label.update(data);
              }

              resultAction.then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
